import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
declare var $: any;
declare var echarts: any;
@Component({
    selector: 'sa-sensorlabor',
    templateUrl: './sensorLabor.component.html',
    styleUrls:['./sensorLabor.component.css']
})
export class SensorLaborComponent implements OnInit {
	
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	carList:any[] = [];
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	deviceId:string = '';//设备ID
	carNumber:string = ''; //车牌号码
	startTime:string = '';
	endTime:string = '';
	carTypeList:any = [];
	carUseList:any = {};
	carDeatailUseList:any = {};
	
	sensorType = '';		//传感器类型
	channel = '';		//人工检测损坏通道
	type:string = '2';	//时间类型
	
	optionList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];	//人工检测损坏通道，1-16
	
	activeSelected:string='';
	startTime_chart:string='';
	endTime_chart:string='';
	isChartView:boolean=false;
	//地图折线组件所需参数 start
	selectedRow:any;//折线子组件获取数据的参数
	drawTrackArr:any;//折线子组件传回   地图子组件画轨迹的数据
	isMapView=false;//折线图子组件选中是否展开地图
	
	specifyId={
		picker1:'datetimepicker3',
		picker2:'datetimepicker4'
	}
	specifyTime={
		startTime:'',
		endTime:'',
	}
	editRowParams:any={		//选择修改的数据
		'carNumber':'',
		'channel':'',
		'createTime':'',
		'createUser':'',
		'deviceId':'',
		'endTime':'',
		'id':'',
		'sensorType':'',
		'startTime':'',
		'updateTime':'',
		'updateUser':'',
	};
	
	
	//地图折线组件所需参数 end
	constructor(private jsonApiService: JsonApiService,private router: Router,private activatedRoute: ActivatedRoute,) {
		activatedRoute.queryParams.subscribe(queryParams => {
			if(queryParams.nowPage){
				this.curPage = queryParams.nowPage;
			}
		});
	}
	//请求传感器类型的接口
	querySensor(){
		this.jsonApiService.http('sensorManage/querySensorList.html','').subscribe(data=> {
			if(data.success){
				this.inputParam = data.data
			}
		})
	}
	inputParam: any = []; 
	//组件传回来 传感器类型
    sensorSelected(data){
        this.sensorType = data.sensorType;
    }
	//获取选中的公司与车辆号码
	getSelectTableTime(time){
		this.startTime=time.startTime;
		this.endTime=time.endTime;
	} 
	getSelectChartTime(time){
		this.startTime_chart=time.startTime;
		this.endTime_chart=time.endTime;
	}
	//地图折线组件方法 start
	runParent(value){
		// 接收子组件传递过来的数据 
	   	this.drawTrackArr=value;
	}
	showMapView(val){
		this.isMapView=val;
	}
	drawAbnormalChart(){
		if(!this.selectedRow){
			this.jsonApiService.message('先选择需要查看折线图的车辆');
			return;
		}
		if(!this.startTime_chart||!this.endTime_chart){
			this.jsonApiService.message('起始时间不能为空');
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime_chart,this.endTime_chart,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let params={
			deviceId:this.selectedRow.deviceId,
			startTime:this.startTime_chart,
			endTime:this.endTime_chart,
		}
		this.lineParamDeal(params);
		// this.selectedRow=params;
	}
	checkAbnormal(row){
		this.activeSelected=row.id;
		// this.startTime_chart=common.getTimeMmss( new Date(new Date(row.startTime).getTime()-5*60*60*1000) );
		// this.endTime_chart = common.getTimeMmss( new Date(new Date(row.endTime?row.endTime:new Date(row.startTime).getTime()+7*60*60*24*1000).getTime()+5*60*60*1000) );
		this.startTime_chart = row.startTime;
		const time = ( new Date(row.startTime).getTime() + 7*(60*60*24*1000) );
		if(row.endTime){
			this.endTime_chart = row.endTime;
		}
		else{
			if(new Date() < new Date(time)){	//获取当前时间
				this.endTime_chart = common.getNowTime();
			}else{								//开始时间往后推七天
				this.endTime_chart = common.getTimeMmss(  new Date( time ) );
			}
		}
		this.isChartView=true;
		this.specifyTime={
			startTime:this.startTime_chart,
			endTime:this.endTime_chart,
		}
		this.lineParamDeal(row);
	}
	
	//获取传感器通道参数
	lineParamDeal(row){
		let reqdata = 'deviceId=' + row.deviceId;
		this.jsonApiService.http("realtime/getRealTimeData.html", reqdata).subscribe(result => {
			if(result.success&&result.data){
				let params = {
					deviceId:row.deviceId,
					startTime:this.startTime_chart,
					endTime:this.endTime_chart,
					coefficient:result.data.coef,
				}
				this.selectedRow = params;
			}
		})
	}
	
	del_chartDate(){
		this.startTime_chart = '';
		this.endTime_chart ='';
	}
	closeChart(){
		this.isChartView=false;
	}

	editRow(row){
		this.editRowParams = row;
		// for (let item in this.editRowParams){
		// 	this.editRowParams[item]=row[item];
		// }
	}
	//保存
	submitUpdate(val){
		const reqdata = {
			'id':this.editRowParams.id,
			'startTime':this.editRowParams.startTime,
			'endTime':this.editRowParams.endTime,
			'carNumber':this.editRowParams.carNumber,
			'deviceId':this.editRowParams.deviceId,
			'channel':this.editRowParams.channel,
		}
		this.jsonApiService.http_obj("sensorMarkDamage/updateSensorMarkDamage.html",reqdata).subscribe(result=> {
			this.jsonApiService.message(result.message);
			if(result.success){
				this.load();
				val.hide();
			}
		})
	}

	ngOnInit() {
		if(this.inputParam.length == 0){this.querySensor();}// 请求传感器列表接口
		this.load();
		this.initDate();
	}
	
	//初始化日期
	initDate(){
		const self = this;
		System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
			()=>{System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
				$("#startTime").datetimepicker({//时间插件
					format: "yyyy-mm-dd hh:ii:ss",
				    language: "zh-CN",
				    autoclose: true,
				}).on("changeDate", function(e){
					var date = e.date;
					if(!date){return;}
					var time = common.getTimeMmss(date);
					$("#endTime").datetimepicker("setStartDate", time);
					self.editRowParams.startTime = time;
				});
				$("#endTime").datetimepicker({//时间插件
					format: "yyyy-mm-dd hh:ii:ss",
				    language: "zh-CN",
				    autoclose: true,
				}).on("changeDate", function(e){
					var date = e.date;
					if(!date){return;}
					var time = common.getTimeMmss(date);
					$("#startTime").datetimepicker("setEndDate", time);
					self.editRowParams.endTime = time;
				});
			});
		});
	}

	delete_row(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("sensorMarkDamage/deleteSensorMarkDamage.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//  初始化加载list数据
	load() {
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				startTime:this.startTime,
				endTime:this.endTime,
				searchKey:this.searchKey,
				deviceId:this.deviceId,
				companyId:this.selectedCompanyId,		//公司ID
				companyName:this.selectedCompanyName,	//公司名
				sensorType:this.sensorType,		//传感器类型
				channel:this.channel,			//人工损坏通道
				carNumber:this.carNumber,		//车牌号码
				type:this.type,					//时间类型
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("sensorMarkDamage/sensorMarkDamageForPage.html ", reqdata).subscribe(result => {
			if(!result.isSuccess){
				this.jsonApiService.message(result.message);
				return;
			}
			this.tableData = result.exhibitDatas;
			this.totalCount = result.recordCount;
		});
	}
	
	ngOnDestroy(){
		document.onclick = null;
	}
	//刷新
    refresh(){
    	this.load();
    }

	//查询
	table_search(){
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	
	//公司选中回调
	companySelected(data){
		this.selectedCompanyName = data.companyName;
		this.selectedCompanyId = data.companyId;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
	
	// 导出
	dataExceport(){
		var parameters = {
			startTime:this.startTime,
			endTime:this.endTime,
			searchKey:this.searchKey,
			deviceId:this.deviceId,
			companyId:this.selectedCompanyId,		//公司ID
			companyName:this.selectedCompanyName,	//公司名
			sensorType:this.sensorType,		//传感器类型
			channel:this.channel,			//人工损坏通道
			carNumber:this.carNumber,		//车牌号码
			type:this.type,					//时间类型
		};
		let timeCheck = common.correctTime(parameters, ['type'])
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'sensorMarkDamage/exportSensorMarkDamageList.html')
	}
}
